﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace XLang
{
    /// <summary>
    /// 语法分析器
    /// </summary>
    public class Parser
    {
        #region 数据属性
        /// <summary>
        /// 词法分析器规则表
        /// </summary>
        public List<ParserRule> Rules { get; private set; }

        public Lexer Lexer { get; private set; }
        #endregion

        #region 构造函数
        public Parser(Assembly assembly)
        {
            this.Rules = new List<ParserRule>();
            this.Lexer = new Lexer(assembly);

            Init(assembly);
        }
        protected virtual void Init(Assembly assembly)
        {
            var types = assembly.GetTypes();

            foreach (var type in types)
            {
                if (type.IsAbstract && !type.IsAST()) continue;

                var methods = type.GetMethods().Where(i=>i.Name=="Rule" && i.IsStatic).ToList();
                foreach (var method in methods) {
                    this.Rules.Add(new ParserRule(method));
                }
            }
        }
        #endregion
    }
}
